package com.lily.aidouzixun.ui;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lily.aidouzixun.R;
import com.lily.aidouzixun.util.DataConverter;
import com.lily.aidouzixun.util.MetricUtil;
import com.lily.aidouzixun.util.Validator;

/**
 * 自定义的标题栏
 * 左边：
 * 中：
 * 右边：
 * 使用时，一定要调用setActivity，设置当前TitleBar所在Activity，用于返回
 */
public class TitleBar extends LinearLayout{

    private Button leftOperation; //左边操作Button
    private ImageTextView itvLeft;  //左边的视图
    private LinearLayout titleLayout;  //标题父布局
    private TextView title1;     //标题
    private TextView subTitle;  //子标题
    private ImageView thirdTitle;    //三级标题，一般为下拉图标
    private TextView title2;  //第二行标题
    private ProgressBar progressBar;    //进度条
    private View rightDefaultOperation; //右边默认的操作图标
    private FrameLayout rightOperationLayout; //右边操作父布局
    private ImageView hasUpdate; //更新图标
    private TextView tvUpdateNum;   //更新数量
    private ImageView imgRightOperate;  //右边操作图标

    private Context mContext;
    private Activity curActivity;   //TitleBar所在Activity,主要用于返回

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        findViews(context);
        setLinsteners();
    }

    /**
     * 得到控件，完成初始化
     * @param context
     */
    private void findViews(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.titlebar,this, true);
        leftOperation = (Button) view.findViewById(R.id.title_leftOperation);
        itvLeft = (ImageTextView) view.findViewById(R.id.itv_left);
        titleLayout = (LinearLayout) view.findViewById(R.id.title_title_layout);
        title1 = (TextView) view.findViewById(R.id.title_title1);
        subTitle = (TextView) view.findViewById(R.id.title_subtitle);
        thirdTitle = (ImageView) view.findViewById(R.id.title_thirdtitle);
        title2 = (TextView) view.findViewById(R.id.title_title2);

        rightOperationLayout = (FrameLayout) view.findViewById(R.id.title_rightOperation_layout);
        rightDefaultOperation = (View) view.findViewById(R.id.title_rightOperation_bn);
        imgRightOperate = (ImageView) view.findViewById(R.id.title_rightOperation_img);
        hasUpdate = (ImageView) view.findViewById(R.id.img_update);
        tvUpdateNum = (TextView) view.findViewById(R.id.tv_update_num);
    }

    /**
     * 设置监听
     */
    private void setLinsteners() {
        //标题栏左边的监听-主要是返回
        leftOperation.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(curActivity != null) {
                    curActivity.finish();
                }
            }
        });
    }

    public void setTitle(String str) {
        if(Validator.isEffective(str)) {
            title1.setText(str);
        }
    }

    public void setTitleListener(OnClickListener listener) {
        titleLayout.setOnClickListener(listener);
    }

    public void setTitleClickable(boolean clickable) {
        titleLayout.setClickable(clickable);
    }

    public String getTitle() {
        return title1.getText().toString();
    }

    public void setSubTitle(String str) {
        subTitle.setText(str);
        subTitle.setVisibility(View.VISIBLE);
    }

    public String getSubTitle() {
        return subTitle.getText().toString();
    }

    public void setTitle2(String title) {
        title2.setText(title);
        title2.setVisibility(View.VISIBLE);
    }

    public String getTitle2() {
        if (title2 != null) {
            return title2.getText().toString();
        } else {
            return null;
        }
    }

    /**
     * 得到第三个标题，即上下箭头
     * @return
     */
    public ImageView getThirdTitle() {
        return thirdTitle;
    }

    public void showThirdTitle(boolean show) {
        if(show)
            thirdTitle.setVisibility(View.VISIBLE);
        else
            thirdTitle.setVisibility(View.GONE);
    }

    /**
     * default, when click leftOperation, will call activity.finish()
     * @param activity
     */
    public void setCurActivity(Activity activity) {
        curActivity = activity;
    }

    public void setLeftOperation(String str) {
        leftOperation.setText(str);
    }
    /**
     * 得到左边操作栏
     * @return
     */
    public Button getLeftOperation() {
        return leftOperation;
    }

    public void setLeftOperationClickListener(OnClickListener listener) {
        leftOperation.setOnClickListener(listener);
    }

    public void setLeftOperation(String str, OnClickListener listener) {
        leftOperation.setText(str);
        leftOperation.setOnClickListener(listener);
    }

    public void setLeftOperationVisible(boolean visible) {
        if(visible)
            leftOperation.setVisibility(View.VISIBLE);
        else
            leftOperation.setVisibility(View.INVISIBLE);
    }

    public void setLeftOperationBackgroundResource(int resid) {
        leftOperation.setBackgroundResource(resid);
    }

    public void setLeftOperation(String str, OnClickListener listener, int resid) {
        setLeftOperation(str, listener);
        setLeftOperationBackgroundResource(resid);
    }

    public void setRightOperationVisibility(int visibility) {
        rightDefaultOperation.setVisibility(visibility);
    }

    /**
     * 设置左边的图标可见,左边的button不可见
     */
    public void setLeftITVVisibility() {
        itvLeft.setVisibility(View.VISIBLE);
        leftOperation.setVisibility(View.GONE);
    }

    public void setLeftITVTextSize(float size) {
        itvLeft.setTextSize(size);
    }
    /**
     * 设置左边ImageTextView的图标
     */
    public void setLeftITVImage(int resId) {
        itvLeft.setImageResource(resId);
    }

    /**
     * 设置左边ImageTextView的文字
     */
    public void setLeftITVText(String text) {
        itvLeft.setTextView(text);
    }

    /**
     * 设置左边ImageTextView的文字颜色
     */
    public void setLeftITVTextColor(int colorId) {
        itvLeft.setTextColor(colorId);
    }

    public void setLeftITVClickListener(OnClickListener onClickListener) {
        itvLeft.setOnClickListener(onClickListener);
    }

    public void setRightOperationImageVisible(boolean visible) {
        if(visible){
            imgRightOperate.setVisibility(View.VISIBLE);
        }else{
            imgRightOperate.setVisibility(View.INVISIBLE);
        }
    }

    public void setRightImageClickListener(OnClickListener listener) {
        imgRightOperate.setOnClickListener(listener);
    }

    public void setRightOperation(String str) {
        if(Validator.isEffective(str)){
            rightDefaultOperation.setVisibility(View.VISIBLE);
            setRightOperationVisible(true);
        }
        if(rightDefaultOperation instanceof Button)
            ((Button)rightDefaultOperation).setText(str);
        else if(rightDefaultOperation instanceof TextView)
            ((TextView)rightDefaultOperation).setText(str);
    }

    public void setRightOperationClickListener(OnClickListener listener) {
        rightDefaultOperation.setOnClickListener(listener);
    }


    public void setRightOperation(String str, OnClickListener listener) {
        setRightOperation(str);
        setRightOperationClickListener(listener);
    }

    public void rightOperationPerformClick() {
        if(rightDefaultOperation != null && rightDefaultOperation.isShown()) {
            rightDefaultOperation.performClick();
        }
    }
    public void setRightOperation(ImageView view) {
        if(view != null) {
            rightOperationLayout.removeAllViews();
            imgRightOperate = view;
            rightOperationLayout.addView(imgRightOperate);
            rightOperationLayout.setVisibility(View.VISIBLE);
        }

    }

    public void setRightOperationVisible(boolean visible) {
        if(visible)
            rightOperationLayout.setVisibility(View.VISIBLE);
        else
            rightOperationLayout.setVisibility(View.INVISIBLE);
    }
    public void setRightOperationDisabled(boolean enable){
        if(enable)
            rightOperationLayout.setEnabled(false);
        else
            rightOperationLayout.setEnabled(true);

    }
    public void showProgressBar(){
        progressBar.setVisibility(View.VISIBLE);
    }

    public void hideProgressBar(){
        progressBar.setVisibility(View.GONE);
    }

    public void showHasUpdate(){
        hasUpdate.setVisibility(View.VISIBLE);
    }

    public void hideHasUpdate(){
        hasUpdate.setVisibility(View.INVISIBLE);
    }

    public void showHasUpdateRight(){
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.RIGHT|Gravity.TOP;
        lp.topMargin = MetricUtil.dip2px(mContext, 12);
        hasUpdate.setLayoutParams(lp);
        hasUpdate.setVisibility(View.VISIBLE);
    }

    public void showUpdateRightNum(String num){
        int tmp_num = DataConverter.parseInt(num);
        if(tmp_num > 0){
            tvUpdateNum.setVisibility(View.VISIBLE);
            tvUpdateNum.setText(tmp_num+"");
        }else{
            tvUpdateNum.setVisibility(View.GONE);
        }
    }

    public void setBgColor(int color) {
        if(getChildCount() > 0){
            getChildAt(0).setBackgroundColor(color);
        }
    }
}
